package com.example.good.controller;

import com.example.good.service.ISMSService;
import com.example.good.util.JsonResult;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@CrossOrigin(value = "http://localhost:8086")
@RequestMapping("sms")
@RestController
public class SMSController extends BaseController{
    @Autowired
    private ISMSService smsService;
    @RequestMapping("{username}/{phone}/{templateID}/send")
    public JsonResult<Object> sendSMS(
            @PathVariable String username,
            @PathVariable String phone,
            @PathVariable String templateID) throws TencentCloudSDKException{
        Object data = smsService.sendSMS(username, phone, templateID);
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("{username}/{phone}/{checkNum}/check")
    public JsonResult<Void> checkSMS(
            @PathVariable String username,
            @PathVariable String phone,
            @PathVariable String checkNum,
            HttpSession session){
        Integer uid = smsService.checkSMS(username,phone,checkNum);
        session.setAttribute("good_uid",uid);
        session.setAttribute("good_usn",username);
        return new JsonResult<>(OK);
    }
}
